package com.china08.yxyapi.schedule.meeting;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.entity.db.meeting.VideoMeeting;
import com.china08.yxyapi.repository.db.VideoMeetingRepository;
import com.china08.yxyapi.service.baijiayun.impl.MeetingRoomServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/**
 * 房间定时检查
 * 
 * @author LiuWeidong
 *
 */
@Component
public class MeetingRoomTask {

    @Autowired
    private VideoMeetingRepository videoMeetingRepository;

    @Autowired
    private MeetingRoomServiceImpl meetingRoomServiceImpl;

    /**
     * ' 检查直播房间是否开始
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void meetingRoomStarCheck() {
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, 30);
        Date beforDate = nowTime.getTime();

        List<VideoMeeting> lastLst = videoMeetingRepository.findByStatusAndStartDateBetween(0, new Date(), beforDate);
        if (lastLst.size() > 0) {
            List<VideoMeeting> saveLst = new ArrayList<VideoMeeting>();
            for (VideoMeeting vm : lastLst) {
                Integer status = meetingRoomServiceImpl.onlineStatus(vm);
                if (status == 1) {
                    vm.setStatus(1);
                    saveLst.add(vm);
                }
            }
            videoMeetingRepository.save(saveLst);
        }
        List<VideoMeeting> beforLst = videoMeetingRepository.findByStatusAndStartDateLessThanEqual(0, new Date());
        if (beforLst.size() > 0) {
            for (VideoMeeting vm : beforLst) {
                vm.setStatus(1);
            }
            videoMeetingRepository.save(beforLst);
        }
    }

    /**
     * 检查直播房间是否结束
     */
    @Scheduled(cron = "0 1/10 * * * ?")
    public void meetingRoomEndCheck() {
        List<VideoMeeting> vmLst = videoMeetingRepository.findByStatusAndEndDateLessThanEqual(1, new Date());
        if (vmLst.size() > 0) {
            List<VideoMeeting> saveLst = new ArrayList<VideoMeeting>();
            for (VideoMeeting vm : vmLst) {
                Integer status = meetingRoomServiceImpl.onlineStatus(vm);
                if (status == 1) {
                    continue;
                }
                vm.setStatus(2);
                meetingRoomServiceImpl.meetingRoomDel(vm.getId());
                vm.setActualEndDate(new Date());
                saveLst.add(vm);
            }
            videoMeetingRepository.save(saveLst);
        }
    }

    /**
     * 获取会议参会人员列表
     */
    @Scheduled(cron = "0 2/10 * * * ?")
    public void meetingRoomReport() {
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, -120);
        Date beforDate = nowTime.getTime();
        List<VideoMeeting> vmLst = videoMeetingRepository.findByEnd(DateUtils.formatDate8(new Date()),
                DateUtils.formatDate8(beforDate));
        if (vmLst.size() > 0) {
            for (VideoMeeting videoMeeting : vmLst) {
                Integer count = meetingRoomServiceImpl.getRoomReport(videoMeeting.getRoomId());
                videoMeeting.setActualPeopleNum(count);
                videoMeetingRepository.save(videoMeeting);
            }
        }
    }
}
